import QtQuick 2.6
import QtQuick.XmlListModel 2.0
import Qt.labs.controls 1.0
ListView {
    id: listview
    property string parent_id: ""
    property string areas: ""
    function pop() {
        StackView.view.pop()
    }
    function push(properties) {
        StackView.view.push("../lib/AddressArea.qml", properties)
    }

    model: XmlListModel{
        id: xmlModel
        source: "qrc:/data/area.xml"
        query: "/areas/area[type=3 and parent_id=%1]".arg(parent_id)
        XmlRole { name: "name"; query: "name/string()" }
        XmlRole { name: "id"; query: "id/string()" }
    }
    header: Rectangle {
        width: listview.width
        height: iHEIGHT * 1.2
        Label {
            anchors.left: parent.left
            anchors.leftMargin: iWIDTH
            anchors.verticalCenter: parent.verticalCenter
            text: areas
        }

        Rectangle {
            color: "forestgreen"
            width: parent.width
            height: 1
            anchors.bottom: parent.bottom
        }
        MouseArea {
            anchors.fill: parent
            onClicked: listview.pop()
        }
    }

    delegate: Rectangle{
        width: listview.width
        height: iHEIGHT * 1.5
        Label {
            text: model.name
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: parent.left
            anchors.leftMargin: iWIDTH
        }

        Rectangle {
            color: "gainsboro"
            width: parent.width
            height: 1
            anchors.bottom: parent.bottom
            visible: index != listview.count - 1
        }

        MouseArea {
            anchors.fill: parent
            onClicked: listview.push({parent_id: model.id, areas: areas + model.name})
        }
    }
}
